package mianyang.zhangsan.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mianyang.zhangsan.com.mapper.UserMapper;
import mianyang.zhangsan.com.pojo.User;
import mianyang.zhangsan.com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 根据用户名查找用户信息
     * @param username
     * @return
     */
    @Override
    public User queryUserInfo(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", username);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    /**
     * 分页查询所有用户信息
     * @param pageNO
     * @return
     */
    @Override
    public Page<User> queryUserInfoList(int pageNO) {
        Page<User> userPage = userMapper.selectPage(new Page<>(pageNO, 3), null);
        return userPage;
    }

    /**
     * 根据用户名删除一个用户
     * @param username
     */
    @Override
    public void deleteUser(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", username);
        userMapper.delete(wrapper);
    }

    /**
     * 根据用户名删除多个用户
     * @param usernames
     */
    @Override
    public void deleteUsers(String usernames) {
        String[] username = usernames.split(",");
        for (String name : username) {
            deleteUser(name);
        }
    }

    /**
     * 新增一个用户
     * @param user
     */
    @Override
    public void addUser(User user) {
        userMapper.insert(user);
    }

    /**
     * 根据条件分页查询用户信息
     * @param pageNo
     * @param pageSize
     * @param user
     * @return
     */
    @Override
    public Page<User> queryPageByWrapper(int pageNo, int pageSize, User user) {
        QueryWrapper<User> wrapper = queryWrapper(user);
        Page<User> userPage = userMapper.selectPage(new Page<>(pageNo, pageSize), wrapper);
        return userPage;
    }

    /**
     * 查询所有的用户信息
     * @return
     */
    @Override
    public List<User> queryList() {

        return userMapper.selectList(null);
    }

    /**
     * 疯转查询条件
     * @param user
     * @return
     */
    @Override
    public QueryWrapper<User> queryWrapper(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (user.getUsername() != null && !"".equals(user.getUsername())){
            wrapper.eq("user_name", user.getUsername());
        }
        if (user.getName() != null &&  !"".equals(user.getName())){
            wrapper.eq("name", user.getName());
        }
        if (user.getUsername() != null &&  !"".equals(user.getUsername())){
            wrapper.eq("username", user.getUsername());
        }
        if (user.getBirthday() != null && !"".equals(user.getBirthday())){
            wrapper.eq("birthDate", user.getBirthday());
        }
        if (user.getUsername() != null && !"".equals(user.getUsername())){
            wrapper.eq("username", user.getUsername());
        }
        if (user.getCardNumber() != null &&  !"".equals(user.getCardNumber())){
            wrapper.eq("cardNumber", user.getCardNumber());
        }
        if (user.getTelephone() != null && !"".equals(user.getTelephone())){
            wrapper.eq("telephone", user.getTelephone());
        }
        return wrapper;
    }

    /**
     * 根据条件查询所有用户
     * @param user
     * @return
     */
    @Override
    public List<User> queryListByWrapper(User user) {
        QueryWrapper<User> wrapper = queryWrapper(user);
        return userMapper.selectList(wrapper);
    }
}
